package com.video;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class VideoAction extends BaseAction   {
	

	public String getFlvVideo(){
		    String videoRoot = request.getRealPath("/");
			String filePath = request.getParameter("f");
			File file = new File(videoRoot, filePath);
			
			 // HTTP/1.1 + IE extensions
			response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
	        // HTTP/1.0
			response.setHeader("Pragma", "no-cache");
	        // Last resort for those that ignore all of the above
			//response.setHeader("Expires", "Sat, 01 Jan 2000 08:00:00 GMT");
			response.setDateHeader("Expires", 0);
			
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "inline;filename=" + file.getName());
			response.setHeader("Content-Length", "" + file.length());
			
	        BufferedInputStream fileIn = null;
	        BufferedOutputStream fileOut = null;
	        byte[] buf = new byte[4096];
	        try {
	            try {
					fileIn = new BufferedInputStream(new FileInputStream(file));
					fileOut = new BufferedOutputStream(response.getOutputStream());
					for (int bytesIn=fileIn.read(buf, 0, buf.length); bytesIn!=-1; bytesIn=fileIn.read(buf, 0, buf.length)) {
					    fileOut.write(buf, 0, bytesIn);
					}
					fileOut.flush();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        } finally {
	            if (fileIn != null) {
	                try { fileIn.close(); } catch(IOException e) {}
	            }
	            if (fileOut != null) {
	                try { fileOut.close(); } catch(IOException e) {}
	            }
	        }
		return null;
	}
	
	public String getVideo(){
		System.out.println("aaaa");
		return SUCCESS;
	}



}
